<?php

namespace App\Http\Requests;

use App\Common\Status;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class InterviewRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'job_id'        => 'required|integer',
            'note_id'       => 'required|integer',
            'address'       => 'required|string',
            'date'          => 'required|string',
            'link_mobile'   => 'required|regex:/^1[3456789]\d{9}$/',
            'mobile'        => 'required|regex:/^1[3456789]\d{9}$/',
            'link_username' => 'required|string',
            'remark'        => 'nullable|string',
        ];
    }

    public function messages(): array
    {
        return [
            'job_id.required'        => '招聘id',
            'note_id.required'       => '简历匹配结果id',
            'mobile.regex'           => '被推荐用户手机有误',
            'link_mobile.regex'      => '发布人手机有误',
            'link_username.required' => '连续人必须',
            'address.required'       => '地址必须',
            'date.required'          => '时间必须',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException($this->fail(Status::ERROR, $error));
    }

    protected function fail(int $code, array $errors)
    {
        return response()->json(
            [
                'code'   => $code,
                'errors' => $errors,
            ]
        );
    }
}

